package com.bowlong.sql;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import com.bowlong.lang.MyThreadFactory;
import com.bowlong.tool.Lib2;

public class DAOAsync extends Lib2 {
	static Map<String, ScheduledExecutorService> ThreadPool = newConcurrentMap();

	public static ScheduledExecutorService executor(String name) {
		synchronized (ThreadPool) {
			ScheduledExecutorService stse = ThreadPool.get(name);
			if (stse == null) {
				MyThreadFactory mtf = new MyThreadFactory(name, true);
				stse = Executors.newSingleThreadScheduledExecutor(mtf);
			}
			return stse;
		}
	}

	public static void shutdown() {
		synchronized (ThreadPool) {
			Collection<ScheduledExecutorService> entrys = ThreadPool.values();
			for (ScheduledExecutorService entry : entrys) {
				entry.shutdown();
			}
			ThreadPool.clear();
		}
	}

	public static void shutdownNow() {
		synchronized (ThreadPool) {
			Collection<ScheduledExecutorService> entrys = ThreadPool.values();
			for (ScheduledExecutorService entry : entrys) {
				entry.shutdownNow();
			}
			ThreadPool.clear();
		}
	}
}
